using System.Collections.Generic;
using UnityEngine;
namespace Gameplay.PVE.TurnBase
{
    public class TbsOperationManager
    {
        private BattleUnit lastTouchUnit;
        private BattleUnit lastDragTarget;
        public static bool enable = true;
        private Vector4 mapBoundary;
        private int state = 0;//0 普通 1 正在技能选敌 2 正在放障碍物
        private BattleUnit skillUnit;
        
        
        private BattleUnit currentSelectUnit;
        public void Initialize()
        {
            enable = false;
            /*EasyTouch.On_SwipeStart += OnDragStart;
            EasyTouch.On_Swipe += OnDrag;
            EasyTouch.On_SwipeEnd += OnDragEnd;*/

            lastTouchUnit = null;
        }

        private BattleUnit TouchUnitByTouchPosition(Vector2 pos)
        {
            Ray ray = Camera.main.ScreenPointToRay(new Vector3(pos.x, pos.y, 0f));
            int layer = LayerMask.NameToLayer("PveUnit");
            int hitIndex = -1;
            RaycastHit[] array = Physics.RaycastAll(ray, float.PositiveInfinity, 1 << layer);
            for (int i = 0; i < array.Length; i++)
            {
                if (hitIndex == -1 || array[hitIndex].transform.position.y < array[i].transform.position.y)
                {
                    hitIndex = i;
                }
            }
            if (hitIndex > -1)
            {
                RaycastHit raycastHit = array[hitIndex];
                BoxCollider boxCollider = raycastHit.collider as BoxCollider;
                var unitTouch = boxCollider.gameObject.GetComponent<UnitTouch>();
                if (unitTouch != null)
                {
                    var unit = unitTouch.GetUnit();
                    return unit;
                }
            }

            return null;
        }






        private List<Vector2> corners = new List<Vector2>();
        
        public void UpdateMapBoundary(Vector4 boundary)
        {
            corners.Clear();
            mapBoundary = boundary;
            float xmin = mapBoundary.x;
            float xmax = mapBoundary.y;
            float zmin = mapBoundary.z;
            float zmax = mapBoundary.w;
            Vector2 leftTop = new Vector2(xmin,zmax);
            Vector2 leftBottom = new Vector2(xmin,zmin);
            Vector2 rightTop = new Vector2(xmax, zmax);
            Vector2 rightBottom = new Vector2(xmax,zmin);
            corners.Add(leftTop);
            corners.Add(leftBottom);
            corners.Add(rightBottom);
            corners.Add(rightTop);
        }

        public void Update()
        {
            if (Input.GetMouseButton(1))
            {
                //TurnBaseManager.Instance.MoveTo(GetGroundPositionByScreenPosition(Input.mousePosition));
            }
        }
        
        private Vector3 GetGroundPositionByBoundary(Vector3 startPosition,Vector3 targetPosition)
        {
            /*Vector2 start = new Vector2(startPosition.x,startPosition.z);
            Vector2 target = new Vector2(targetPosition.x,targetPosition.z);

            for (int i = 0; i < corners.Count; i++)
            {
                Vector2 cornerStart = corners[i];
                Vector2 cornerEnd;
                if (i == corners.Count - 1)
                {
                    cornerEnd = corners[0];
                }
                else
                {
                    cornerEnd = corners[i + 1];
                }
                var intersection = PveUtils.GetIntersection(start, target, cornerStart, cornerEnd);
                if (intersection != Vector2.zero)
                {
                    return new Vector3(intersection.x, 0, intersection.y) + (startPosition - targetPosition).normalized * 0.2f;
                }
            }*/
            return targetPosition;
        }

        private Vector3 GetGroundPositionByScreenPosition(Vector2 pos)
        {
            Ray ray = Camera.main.ScreenPointToRay(new Vector3(pos.x, pos.y, 0f));
                
            var origin = ray.origin;
            var direction = ray.direction;
            var p = origin.y / direction.y;
            return origin - direction * p;
        }

        public void UnitDead(int id)
        {
            if (currentSelectUnit != null && currentSelectUnit.Data.id == id)
            {
                SelectUnit(-1);
            }
        }
        

        private void SelectUnit(int id,bool callUI = true)
        {
            currentSelectUnit?.Transmit(ETransmitType.OnUnSelected);
            if (id == -1)
            {
                currentSelectUnit = null;
            }
            else
            {
                var unit = TurnBaseManager.Instance.GetUnit(id);
                if (unit != null)
                {
                    unit.Transmit(ETransmitType.OnSelected);
                    currentSelectUnit = unit;
                    if (callUI)
                    {
                        //LuaManager.mainState.GetFunction("pveManager.SelectHero").Call(unit.Data.id);
                    }
                }
            }

            TurnBaseManager.Instance.SetTargetUnit(currentSelectUnit);
        }

        public void Dispose()
        {
        
        }
    }
}